﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WSLDD
{
    public partial class Form1 : Form
    {
        private string ofDisk= "";  // 输出盘符
        private System.Windows.Forms.OpenFileDialog openFileDialog; // 文本打开对话框

        public Form1()
        {
            InitializeComponent();

            // 1.read config
            this.setDataGridView_Values(1);
        }

        private void setDataGridView_Values(int number)
        {
            int index = 0;

            // 清除所有row
            dataGridView_Info.Rows.Clear();

            for (int i = 0; i < number; i++)
            {
                // 行数不够，添加行
                index = dataGridView_Info.Rows.Add();

                // 设置数据
                dataGridView_Info.Rows[index].Cells[0].Value = index+1;
                dataGridView_Info.Rows[index].Cells[1].Value = 33;
                dataGridView_Info.Rows[index].Cells[2].Value = "1K";
                dataGridView_Info.Rows[index].Cells[3].Value = "";
                dataGridView_Info.Rows[index].Cells[4].Value = "...";

                // 设置颜色
                dataGridView_Info.Rows[index].Cells[0].Style.BackColor = Color.DeepSkyBlue;
                dataGridView_Info.Rows[index].Cells[1].Style.BackColor = Color.Cornsilk;
                dataGridView_Info.Rows[index].Cells[2].Style.BackColor = Color.PaleTurquoise;
                dataGridView_Info.Rows[index].Cells[3].Style.BackColor = Color.Cyan;
                dataGridView_Info.Rows[index].Cells[4].Style.BackColor = Color.YellowGreen;
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView_Info_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        // 单元格编辑之后
        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
            // 选择文件
            if(e.ColumnIndex == 4)
            {
                string infile = "";
                dataGridView_Info.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "...";  // 回到编辑前的状态
                infile = dataGridView_Info.Rows[e.RowIndex].Cells[3].Value.ToString();  // 临时保存数据

                //获取所打开文件的文件名
                openFileDialog = new OpenFileDialog();
                DialogResult dr = openFileDialog.ShowDialog();

                // 文件打开成功
                if (dr == System.Windows.Forms.DialogResult.OK)
                    infile = openFileDialog.FileName;

                // 回写文件路径
                dataGridView_Info.Rows[e.RowIndex].Cells[3].Value = infile;
            }

            // 添加新项后数据补全
            if (dataGridView_Info.Rows[e.RowIndex].Cells[0].Value == null)
                dataGridView_Info.Rows[e.RowIndex].Cells[0].Value = dataGridView_Info.RowCount;
            if (dataGridView_Info.Rows[e.RowIndex].Cells[1].Value == null)
                dataGridView_Info.Rows[e.RowIndex].Cells[1].Value = "";
            if (dataGridView_Info.Rows[e.RowIndex].Cells[2].Value == null)
                dataGridView_Info.Rows[e.RowIndex].Cells[2].Value = "512";
            if(dataGridView_Info.Rows[e.RowIndex].Cells[3].Value == null)
                dataGridView_Info.Rows[e.RowIndex].Cells[3].Value = "";
            if (dataGridView_Info.Rows[e.RowIndex].Cells[4].Value == null)
                dataGridView_Info.Rows[e.RowIndex].Cells[4].Value = "...";

        }

        //选择盘符
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ofDisk = comboBox1.Text;
        }

        //选择盘符被点击
        private void ofDisk_comboBox_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear(); // 先清除内容

            // 获取所有盘符
            var drivers = System.IO.DriveInfo.GetDrives();
            foreach (var driver in drivers)
                comboBox1.Items.Add(driver.ToString() + "");
        }

        // 点击导入配置按钮 事件
        private void inConfig_button_Click(object sender, EventArgs e)
        {

        }

        // 点击导出配置按钮 事件
        private void outConfig_button_Click(object sender, EventArgs e)
        {

        }

        // 点击烧录按钮 事件
        private void fusing_button_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            for (int i = 0; i < dataGridView_Info.RowCount - 1; i++) {
                textBox1.Text += "\r\nfusing" + i+1 + ":\r\n";
                // fusing image
                textBox1.Text += Program.fusing(dataGridView_Info.Rows[i].Cells[3].Value.ToString(), ofDisk, 
                     dataGridView_Info.Rows[i].Cells[1].Value.ToString(), dataGridView_Info.Rows[i].Cells[2].Value.ToString());
            }

            // 设置光标位置 到文本最后
            textBox1.SelectionStart = textBox1.TextLength;
            // 随文本滚动 
            textBox1.ScrollToCaret();
        }
    }
}
